package com.jackrain.nea.fc.dl.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.ViewDLPurOrderToF4QueryCmd;
import com.jackrain.nea.fc.dl.mapper.ViewDLPurOrderToF4Mapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author wuwenxue
 * @date 2018/05/22
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class ViewDLPurOrderToF4QueryCmdImpl extends CommandAdapter implements ViewDLPurOrderToF4QueryCmd {

    private final static FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ValueHolder vh = new ValueHolder();
        ViewDLPurOrderToF4Mapper viewDLPurOrderToF4Mapper = ApplicationContextHandle.getBean(
                ViewDLPurOrderToF4Mapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(
                map.get("param"), "yyyy-MM-dd HH:mm:ss"), Feature.OrderedField);
        log.info("ViewDLPurOrderToF4QueryCmdImpl--param-->{}",param);

        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        Object[] ids = param.getJSONArray("IDS").toArray();
        String action = param.getString("action");
        if (null == action || "".equals(action)) {
            return vh;
        }
        JSONArray jsonArray = new JSONArray();

        if ("submit".equals(action) || "save".equals(action) || "add".equals(action)) {
            //保存 || 提交
            Integer statu = null;
            if ("submit".equals(action)) {
                statu = 2;
            } else {
                statu = 1;
            }

            List<HashMap> list = viewDLPurOrderToF4Mapper.selOrderData(ids, statu, null);
            for (HashMap hashMap : list) {
                Long id = TypeUtils.castToLong(hashMap.get("ID"));
                String billNo = TypeUtils.castToString(hashMap.get("BILL_NO"));

                Date billDate = TypeUtils.castToDate(hashMap.get("BILL_DATE"));
                String formatBillDate = DATE_FORMAT.format(billDate);

                hashMap.put("BILL_DATE", formatBillDate);
                Long purReasonID = TypeUtils.castToLong(hashMap.get("PUR_REASON"));
                String purReason = viewDLPurOrderToF4Mapper.selPurReasonEcode(purReasonID);
                hashMap.put("PUR_REASON", purReason);

                List<HashMap> itemList = viewDLPurOrderToF4Mapper.selOrderItemData(id);
                JSONArray itemArray = new JSONArray();
                for (HashMap itemH : itemList) {
                    itemH.put("QTY_OVERLOAD", itemH.get("OVER_ADD_QTY"));
                    itemH.put("BILL_NO", billNo);
                    //管理成本价
                    BigDecimal priceMgt = TypeUtils.castToBigDecimal(itemH.get("PRICE_MGT"));
                    //管理成本金额
                    BigDecimal amtMgt = TypeUtils.castToBigDecimal(itemH.get("AMT_MGT"));
                    //不含税采购价
                    BigDecimal priceNonTax = TypeUtils.castToBigDecimal(itemH.get("PRICE_NON_TAX"));
                    //不含税采购金额
                    BigDecimal amtNonTax = TypeUtils.castToBigDecimal(itemH.get("AMT_NON_TAX"));

                    if (null != priceMgt) {
                        itemH.put("PRICE_MGT", priceMgt);
                    } else {
                        itemH.put("PRICE_MGT", 0);
                    }

                    if (null != amtMgt) {
                        itemH.put("AMT_MGT", amtMgt);
                    } else {
                        itemH.put("AMT_MGT", 0);
                    }

                    if (null != priceNonTax) {
                        itemH.put("PRICE_NON_TAX", priceNonTax);
                    } else {
                        itemH.put("PRICE_NON_TAX", 0);
                    }

                    if (null != amtNonTax) {
                        itemH.put("AMT_NON_TAX", amtNonTax);
                    } else {
                        itemH.put("AMT_NON_TAX", 0);
                    }
                    JSONObject itemjs = new JSONObject(itemH);
                    itemArray.add(itemjs);
                }

                JSONObject js = new JSONObject(hashMap);
                js.put("DL_B_PUR_ORDER_ITEM", itemArray);
                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        } else if ("unsubmit".equals(action)) {
            List<HashMap> list = viewDLPurOrderToF4Mapper.selOrderData(ids, 1, null);

            for (HashMap hashMap : list) {
                JSONObject unSumJson = new JSONObject();
                unSumJson.put("BILL_TYPE", "PUR_ORDER");
                unSumJson.put("BILL_NO", hashMap.get("BILL_NO"));
                unSumJson.put("STATUS_TYPE", "UNCHK");
                unSumJson.put("OPR_ECODE", hashMap.get("MODIFIERNAME"));
                unSumJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));
                unSumJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));

                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(unSumJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        } else if ("terminat".equals(action)) {
            //终止
            List<HashMap> list = viewDLPurOrderToF4Mapper.selOrderData(ids, 2, null);

            for (HashMap hashMap : list) {
                JSONObject terJson = new JSONObject();
                terJson.put("BILL_TYPE", "PUR_ORDER");
                terJson.put("BILL_NO", hashMap.get("BILL_NO"));
                terJson.put("STATUS_TYPE", "TERMINAT");
                terJson.put("OPR_ECODE", hashMap.get("MODIFIERNAME"));
                terJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));
                terJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));
                terJson.put("USERDEFINE1", hashMap.get("TERMINATE_TYPE"));

                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        } else if ("unterminat".equals(action)) {
            //取消终止
            List<HashMap> list = viewDLPurOrderToF4Mapper.selOrderData(ids, 2, null);

            for (HashMap hashMap : list) {
                JSONObject terJson = new JSONObject();
                terJson.put("BILL_TYPE", "PUR_ORDER");
                terJson.put("BILL_NO", hashMap.get("BILL_NO"));
                terJson.put("STATUS_TYPE", "UNTERMINAT");
                terJson.put("OPR_ECODE", hashMap.get("MODIFIERNAME"));
                terJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));
                terJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));

                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        } else if ("void".equals(action)) {
            //作废
            List<HashMap> list = viewDLPurOrderToF4Mapper.selOrderData(ids, null, "N");

            for (HashMap hashMap : list) {
                JSONObject terJson = new JSONObject();
                terJson.put("BILL_TYPE", "PUR_ORDER");
                terJson.put("BILL_NO", hashMap.get("BILL_NO"));
                terJson.put("STATUS_TYPE", "DEL");
                terJson.put("OPR_ECODE", hashMap.get("MODIFIERNAME"));
                terJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));
                terJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));

                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        } else if ("modify".equals(action)) {
            //预计到仓日期
            List<HashMap> list = viewDLPurOrderToF4Mapper.selOrderData(ids, null, null);

            for (HashMap hashMap : list) {
                JSONObject terJson = new JSONObject();
                terJson.put("BILL_TYPE", "PUR_ORDER");
                terJson.put("BILL_NO", hashMap.get("BILL_NO"));
                terJson.put("STATUS_TYPE", "MODIFY");
                terJson.put("USERDEFINE1", hashMap.get("ESTIMATED_DELIVERY_DATE"));
                terJson.put("OPR_ECODE", hashMap.get("MODIFIERNAME"));
                terJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));
                terJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));

                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        }else if ("modify_date_in".equals(action)) {
            //承诺交期
            List<HashMap> list = viewDLPurOrderToF4Mapper.selOrderData(ids, null, null);

            for (HashMap hashMap : list) {
                JSONObject terJson = new JSONObject();
                terJson.put("BILL_TYPE", "PUR_ORDER");
                terJson.put("BILL_NO", hashMap.get("BILL_NO"));
                terJson.put("STATUS_TYPE", "MODIFY_DATE_IN");
                terJson.put("USERDEFINE1", hashMap.get("DATE_IN"));
                terJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));
                terJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));

                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        }  else {
            log.debug("ViewDLPurOrderToF4QueryCmdImpl_action--action-->{}",action);
//            log.debug("ViewDLPurOrderToF4QueryCmdImpl_action:" + action);
        }

        vh.put("data", jsonArray);
        return vh;
    }


}
